document.querySelector("html").style.fontSize = (window.innerWidth / 16) + "px";
window.onorientationchange = () => {
  window.location.reload();
};

const opt = {
  container: document.getElementById("player"),
  loop: true,
  screenshot: true,
  hotkey: true,
  airplay: true,
  // preload: "auto",
  logo: "/favicon.ico",
  video: {},
};
var dp = false;
$(function () {
  init()
  rate()

  var count = 0
  function init() {
    var value = getData().value

    // sessionStorage.setItem('video_id', value);
    // console.log(value)
    var data = {
      'video_id': value
    }
    // console.log(JSON.stringify(data))
    data = JSON.stringify(data)
    $.ajax({
      url: 'https://jdb-demo.4ukids.cn/api/video/detail',
      type: 'post',
      data: data,
      headers: {
        'ACCESS-TOKEN': '0F242BEB-33A4-7CFB-BA85-950EC16F8485',
      },
      contentType: 'application/json',
      datatype: 'json',
      // async: false,
      success: function (res) {
        var Vcode = '';
        res = JSON.parse(res);
        res.data.video_url = "https" + res.data.video_url.substr(4);
        opt.video = {
          url: res.data.video_url,
          pic: getData().img,
          // pic: "/img/061201.png"
        }

        // console.log(dp);
        try {
          dp = new DPlayer(opt);
          dp.on("loadeddata", () => {
            let t = window.innerHeight * 0.8;
            if (opt.container.clientHeight > t) opt.container.style.height = t + "px";
          });
        } catch (error) {
          alert(error);
          alert(res.data.video_url);
        }
        // '<video src="' + res.data.video_url + '" controls="controls" class="fill" poster="' + getData().img + '"></video>' +
        const btn = $(".bottombtn");
        count = res.data.video_rank
        Vcode += '<div class="videotop">' +
          '<div class="videotitle">' + res.data.video_name_cn + '</div>' +
          '<div class="videorate"></div>' +
          '</div>' +
          '<div class="videointro">' +
          '<p>' + res.data.video_desc + '</p>' +
          '</div>'
        btn.before(Vcode);
        btn.click(function () {
          var ddata = {
            title: res.data.video_name_cn,
            value: value,
            rank: res.data.video_rank,
            pic: opt.video.pic
          }
          ddata = encodeURIComponent(JSON.stringify(ddata));
          // console.log(ddata)
          window.location = './dubbing.html?data=' + ddata;
        })
      },
      error(err) {
        alert(7);
        alert(err);
      }
    })
  }

  function getData() {
    var urlinfo = window.location.href,
      value = urlinfo.split("?")[1].split("=")[1];
    // console.log(JSON.parse(decodeURI(value)))
    return JSON.parse(decodeURI(value))
  }

  function rate() {
    var star_fill = '<img class="rate" src="img/star_fill.png"/>'
    var star_empty = '<img class="rate" src="img/star_empty.png"/>'
    var star_rate = '级别&nbsp;'
    for (let i = 0; i < 5; i++) {
      if (i < count) {
        star_rate += star_fill
      }
      else {
        star_rate += star_empty
      }
    }
    // console.log(star_rate)
    $(".videorate").append(star_rate)
  }
})